/**
 * Created by superman on 17/2/16.
 * http配置
 */
 import axios from 'axios'
 import config from '../config/base.config'
 import ElementUI from 'element-ui';
 import cloudbase from '@cloudbase/js-sdk'

  // 初始化云环境
  const app = cloudbase.init({
    env: 'web-cloud0713-6g519i65f479e638',
  })
 // axios 配置 15s
 axios.defaults.timeout = 6000*1000
 axios.defaults.baseURL = config.api_url
 const auth = app.auth();
 
 const http = {
    get(dbName, params) {
      return new Promise((resolve, reject) => {
          //  显示等待框
        const loadings = ElementUI.Loading.service({
            lock: true,
            text: '加载中，请稍候。。。',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.7)'
        });
        auth.anonymousAuthProvider().signIn().then(()=>{
          auth.getLoginState().then(data=>{
            if(data.isAnonymousAuth){
              app.callFunction({name:dbName,data:params}).then(res => {
                  resolve(res.result);
                  loadings.close();
              }).catch(function (error) {
                  alert(error)
                  loadings.close();
                  reject(error);
              });
            }
          })
        })
        
      });
    }
 }
 
 export default http